package com.qqt.csr.route.service;

import com.google.common.collect.Lists;
import com.qqt.csr.common.utils.CacheKeyUtil;
import com.qqt.csr.route.dto.resp.RouteResultDTO;
import com.qqt.csr.route.dto.resp.SkillSetDTO;
import com.qqt.csr.route.entity.RouteTask;
import com.qqt.csr.route.enums.RouteTaskEventEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RScoredSortedSet;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 访客排队
 */
@Slf4j
@Service
public class VisitorQueueService {
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 访客入队
     *
     * @param routeTask
     */
    public void add(RouteTask routeTask, RouteResultDTO resultDTO) {
        SkillSetDTO skillSetConfig = routeTask.getSkillSet();
        if (skillSetConfig == null) {
            return;
        }

        String cacheKey = String.format(CacheKeyUtil.CsrRoute.DISTRIBUTION_SKILL_SET_QUEUE, skillSetConfig.getSkillSetId());
        RScoredSortedSet<String> scoredSortedSet = redissonClient.getScoredSortedSet(cacheKey);

        long score = Long.parseLong("100001" + System.currentTimeMillis());
        scoredSortedSet.add(score, routeTask.getId());

        redissonClient.getSet(CacheKeyUtil.CsrRoute.DISTRIBUTION_SKILL_SET_QUEUE_LIST).add(skillSetConfig.getSkillSetId().toString());

        int num = scoredSortedSet.size() - 1;
        resultDTO.setContent(routeTask.getRouteRule().buildQueueTip(num));
        routeTask.addEvent(RouteTaskEventEnum.IN_QUEUE);
        routeTask.setInQueueTime(new Date());
    }

    /**
     * 返回队列数量
     *
     * @param skillSetId
     * @return
     */
    public int size(Long skillSetId) {
        String cacheKey = String.format(CacheKeyUtil.CsrRoute.DISTRIBUTION_SKILL_SET_QUEUE, skillSetId);
        return redissonClient.getScoredSortedSet(cacheKey).size();
    }

    /**
     * 移除排队
     *
     * @param skillSetId
     * @param routeTaskIdList
     */
    public void remove(Long skillSetId, List<String> routeTaskIdList) {
        String cacheKey = String.format(CacheKeyUtil.CsrRoute.DISTRIBUTION_SKILL_SET_QUEUE, skillSetId);
        RScoredSortedSet<String> scoredSortedSet = redissonClient.getScoredSortedSet(cacheKey);
        scoredSortedSet.removeAll(routeTaskIdList);
    }

    /**
     * 移除排队
     *
     * @param skillSetId
     * @param routeTaskId
     */
    public void remove(Long skillSetId, String routeTaskId) {
        this.remove(skillSetId, Lists.newArrayList(routeTaskId));
    }

    /**
     * 获取所有队列的技能组id
     *
     * @return
     */
    public List<Long> getSkillSetQueueList() {
        return redissonClient.getSet(CacheKeyUtil.CsrRoute.DISTRIBUTION_SKILL_SET_QUEUE_LIST).readAll()
                .stream().map(skillSetId -> Long.parseLong(skillSetId.toString())).toList();
    }

    /**
     * 获取指定技能组队列排队访客会话标识id
     *
     * @param skillSetId
     * @return
     */
    public List<String> poll(Long skillSetId, int startIndex, int endIndex) {
        String cacheKey = String.format(CacheKeyUtil.CsrRoute.DISTRIBUTION_SKILL_SET_QUEUE, skillSetId);
        RScoredSortedSet<String> scoredSortedSet = redissonClient.getScoredSortedSet(cacheKey);
        return scoredSortedSet.valueRange(startIndex, Math.min(endIndex, scoredSortedSet.size() - 1))
                .stream().collect(Collectors.toList());
    }
}
